package com.huluobo.high3demo.video;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvm.baen.BannerBeanResult;
import com.huluobo.mvvm.baen.CommentBeanResult;
import com.huluobo.mvvm.baen.FollowLikeBeanResult;
import com.huluobo.mvvm.baen.VideoBeanResult;
import com.huluobo.mvvm.model.BaseRepo;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * Created by LC on 2023/9/15.
 */
public class VideoRepo extends BaseRepo {
    public void getVideoList(int currentPage, int pageSize, MutableLiveData<VideoBeanResult> success,
                             MutableLiveData<String> failed) {
        apiService.getVideoList(currentPage, pageSize).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(VideoBeanResult videoBeanResult) {
                        success.setValue(videoBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getBanner(MutableLiveData<BannerBeanResult> success, MutableLiveData<String> failed) {
        apiService.getBanner().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BannerBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BannerBeanResult bannerBeanResult) {
                        success.setValue(bannerBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void likeVideo(int videoId, MutableLiveData<FollowLikeBeanResult> success, MutableLiveData<String> failed) {
        apiService.likeVideo(videoId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowLikeBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FollowLikeBeanResult followLikeBeanResult) {
                        success.setValue(followLikeBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void noLikeVideo(int videoId, MutableLiveData<FollowLikeBeanResult> success,
                            MutableLiveData<String> failed) {
        apiService.noLikeVideo(videoId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowLikeBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FollowLikeBeanResult followLikeBeanResult) {
                        success.setValue(followLikeBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void followAuth(String authName, MutableLiveData<FollowLikeBeanResult> success,
                           MutableLiveData<String> failed) {
        apiService.followAuth(authName).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowLikeBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FollowLikeBeanResult followLikeBeanResult) {
                        success.setValue(followLikeBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 发送评论
     *
     * @param msg     消息内容
     * @param videoId 视频id
     * @param success 成功的livedata
     * @param failed  失败的livedata
     */
    public void sendComment(RequestBody body, MutableLiveData<CommentBeanResult> success,
                            MutableLiveData<String> failed) {
        apiService.sendComment(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommentBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CommentBeanResult commentBeanResult) {
                        success.setValue(commentBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取评论列表
     *
     * @param videoId 视频id
     * @param success 成功的livedata
     * @param failed  失败的livedata
     */
    public void getCommentList(int videoId, MutableLiveData<CommentBeanResult> success,
                               MutableLiveData<String> failed) {
        apiService.getCommentList(videoId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommentBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CommentBeanResult commentBeanResult) {
                        success.setValue(commentBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
